package com.calvin.study.service.sys.security;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreFilter;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import com.calvin.study.mapper.sys.SysUserMapper;
import com.calvin.study.model.sys.SysUser;

@Service
public class MethodELService {
	
	@Autowired
	private SysUserMapper sysUserMapper;
	
	@PreAuthorize("hasRole('admin')")
	public List<SysUser> findAll(){
		return sysUserMapper.selectList(null);	
	}
	
	@PostAuthorize("returnObject.userName == authentication.name")
	public SysUser findUser() {
		String authName = SecurityContextHolder.getContext().getAuthentication().getName();
		System.out.println(authName);
		return new SysUser("admin");
		
	}
	
	@PreFilter(filterTarget="ids",value="filterObject%2==0")
	public void delete(List<Integer> ids) {
		System.out.println(ids);
	}
	
	@PostFilter("filterObject.userName == authentication.name")
	public List<SysUser> findAllPD(){
		List<SysUser> sysUsers = new ArrayList<SysUser>();
		sysUsers.add(new SysUser("kobe"));
		sysUsers.add(new SysUser("admin"));
		return sysUsers;
	}

}
